package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.ve.EditorHolder;
import com.stark.ve.core.IEditorListener;
import com.stark.ve.gif.ShowGifActivity;
import com.warkiz.widget.IndicatorSeekBar;
import com.warkiz.widget.OnSeekChangeListener;
import com.warkiz.widget.SeekParams;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.ColorAdapter;
import flc.ast.databinding.ActivityVideoGifBinding;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.ProgressConvertUtil;

public class VideoGifActivity extends BaseAc<ActivityVideoGifBinding> {

    public static String sVideoPath;
    private int mFrame = 10;
    private float mSpeed = 1.0f;
    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            if (mDataBinding.videoView.isPlaying()) {
                mDataBinding.tvDuration.setText(TimeUtils.millis2String(mDataBinding.videoView.getCurrentPosition(), getString(R.string.duration_style)) + "/" + TimeUtils.millis2String(MediaUtil.getDuration(sVideoPath), getString(R.string.duration_style)));
                mDataBinding.sbProgress.setProgress(mDataBinding.videoView.getCurrentPosition());
            }
            mHandler.postDelayed(mTaskUpdateTime, 500);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.activity_video_gif;
    }

    @Override
    protected void initView() {
        mDataBinding.ivVideoGifBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivPlay.setOnClickListener(this);
        mDataBinding.tvVideoGifConfirm.setOnClickListener(this);

        mHandler = new Handler();
        mDataBinding.sbProgress.setMax((int) MediaUtil.getDuration(sVideoPath));
        mDataBinding.tvDuration.setText("00:00" + "/" + TimeUtils.millis2String(MediaUtil.getDuration(sVideoPath), getString(R.string.duration_style)));
        mDataBinding.videoView.setVideoPath(sVideoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {  // 播放完成的监听
                mDataBinding.tvDuration.setText(TimeUtils.millis2String(MediaUtil.getDuration(sVideoPath), getString(R.string.duration_style)) + "/" + TimeUtils.millis2String(MediaUtil.getDuration(sVideoPath), getString(R.string.duration_style)));
                mDataBinding.ivPlay.setSelected(false);
                mp.seekTo(1);
                stopTime();
            }
        });
        mDataBinding.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mDataBinding.sbProgress.setMax(mp.getDuration());
                mDataBinding.sbProgress.setProgress(mp.getCurrentPosition());
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPlay://播放
                if (mDataBinding.videoView.isPlaying()) {
                    mDataBinding.ivPlay.setSelected(false);
                    mDataBinding.videoView.pause();
                    stopTime();
                } else {
                    mDataBinding.ivPlay.setSelected(true);
                    mDataBinding.videoView.start();
                    startTime();
                }
                break;
            case R.id.tvVideoGifConfirm://保存
                save();
                break;
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvInterval.setText(getString(R.string.interval_tip, mFrame));
        mDataBinding.sbInterval.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mFrame = (int) ProgressConvertUtil.progress2value(progress, 5, 15, 100);
                mDataBinding.tvInterval.setText(getString(R.string.interval_tip, mFrame));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        mDataBinding.sbSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mSpeed = ProgressConvertUtil.progress2value(progress, 0.3f, 2.0f, 100);
                mDataBinding.tvInterval.setText(getString(R.string.interval_tip, mFrame));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void save() {
        int scaleWidth = mDataBinding.videoView.getWidth();
        int scaleHeight = mDataBinding.videoView.getHeight();
        if (scaleWidth * scaleHeight > 960 * 540) {
            //原视频分辨率过大，则要缩小一倍
            scaleWidth = scaleWidth / 2;
            scaleHeight = scaleHeight / 2;
        }
        showDialog(getString(R.string.ve_handle_percent_format, 0 + "%"));
        EditorHolder.editor().video2Gif(sVideoPath, scaleWidth, scaleHeight, mFrame, mSpeed, new IEditorListener() {
            @Override
            public void onSuccess(String outFilePath) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.save_success));
                FileP2pUtil.copyPrivateImgToPublic(mContext, outFilePath);
                startActivity(HomeActivity.class);
            }

            @Override
            public void onFailure(String errMsg) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.ve_covert_gif_fail_tip));
            }

            @Override
            public void onProgress(int progress) {
                showDialog(getString(R.string.ve_handle_percent_format, progress + "%"));
            }
        });
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopTime();
    }


    @Override
    protected void onResume() {
        super.onResume();
        mDataBinding.videoView.seekTo(1);
    }

//    R.layout.fragment_ve_gif_operation


}